Page({

  /**
   * 页面的初始数据
   */
  data: {
    sorttype: 0,
    hasMore: true,
    page: 1,
    list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.showLoading({
      title: '加载数据...',
      mask: true
    });
    this.loadList();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page: 1,
      hasMore: true
    });
    this.loadList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("bottom");
    if (this.data.loading || !this.data.hasMore) {
      return false;
    } else {
      this.setData({
        loading: true
      });
      this.loadList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },

  loadList: function () {//加载帖子
    // wx.showLoading({
    //   title: '正在加载',
    //   mask: true
    // });
    var USER_ID = wx.getStorageSync("USER_ID");
    if (!USER_ID) {
      wx.showModal({
        title: '提示',
        content: '未获取您的用户ID，请关联您的账号！',
      });
      return false;
    }
    var that = this,
      page = this.data.page,
      sta = this.data.sorttype;

    console.log(sta)

    wx.request({
      url: 'https://m.nongjike.cn/NJK/app/findleaving_Message',
      data: {
        USER_ID: USER_ID,
        pageNum: page,
        YSTATUS: sta
      },
      success: function (res) {
        wx.stopPullDownRefresh();
        var data = res.data;
        console.log(data);
        if (data.code == 1) {
          that.setData({
            img: data.IMG,
          });

          var oldlist = that.data.list;

          if (!data.data || !data.data.object || data.data.object.length <= 0) {//没有数据了
            that.setData({
              loading: false,
              hasMore: false,
            })
            wx.hideLoading();
            return false;
          }

          if (page == 1) {//第一页内容，替换之前的list
            page++;
            var newlist = data.data.object,
              sortedlist = [];
            for (var i = 0; i < newlist.length; i++) {
              if (newlist[i].ZSTATUS == 1) {
                sortedlist.unshift(newlist[i]);
              } else {
                sortedlist.push(newlist[i]);
              }
            }

            that.setData({
              list: sortedlist,
              page: page,
              loading: false,
              hasMore: true
            })
          } else {//非第一页内容，加在前面的list中
            page++;
            var newlist = data.data.object,
              sortedlist = [];
            for (var i = 0; i < newlist.length; i++) {
              if (newlist[i].ZSTATUS == 1) {
                oldlist.unshift(newlist[i]);
              } else {
                oldlist.push(newlist[i]);
              }
            }

            that.setData({
              list: oldlist,
              page: page,
              loading: false,
              hasMore: true
            })
          }
        } else {
          wx.showModal({
            title: '提示',
            content: data.message,
          })
        }
        wx.hideLoading()
      }
    })

  },

  showsort: function () {
    this.setData({
      showsort: !this.data.showsort,
    })
  },

  hiddensort: function () {
    this.setData({
      showsort: false,
    })
  },

  selsort: function (e) {
    console.log(e.currentTarget.dataset.id);
    this.setData({
      sorttype: e.currentTarget.dataset.id,
      showsort: false,
      hasMore: true,
      page: 1
    });
    this.loadList();
  },

  showmenu: function () {
    this.setData({
      showmenu: true
    })
  },

  hiddenmenu: function () {
    this.setData({
      showmenu: false
    })
  },

  menu: function (e) {
    var id = e.currentTarget.dataset.id;
    if (id == 3) {
      if (!this.data.showmenu) {
        this.setData({
          showmenu: true
        });
        return false;
      }
    }

    console.log(id);
  },

  navto: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/allarticle/allarticle',
    })
  },

  navi:function(e){
    wx.navigateTo({
      url: '/pages/commentdetail/commentdetail?id='+e.currentTarget.dataset.id,
    })
  },

  comipt: function (e) {
    this.setData({
      ipt: e.detail.value
    });
  },


  sendcom: function () {
    var val = this.data.ipt;
    var that = this;
    if (!val || val.length <= 0) {
      return false;
    }

    wx.request({
      url: 'https://m.nongjike.cn/NJK/app/saveleaving_Message',
      method: "POST",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: {
        USER_ID: wx.getStorageSync("USER_ID"),
        MESSAGE: val
      },
      success: function (res) {
        var data = res.data;
        console.log(data);
        if (data.code == 1) {
          wx.showToast({
            title: '评论成功！',
          });
          that.setData({
            page: 1,
            hasMore: true,
          });
          that.loadList();
        }
      }
    })
  }
})